﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Atom.Entity.Medicare;
using Atom.DAL.Medicare;
using Atom.Entity.Medicare.Model;
using System.Data.Common;
using Atom.Entity;
using Atom.DAL.HIS;
using Atom.Entity.Medicare.DAL;
using Atom.DAL;

namespace Atom.BLL.Medicare
{
    public class BAZABLL : BaseBLL
    {
        public List<BAZAEntity> GetAllEntityByDate(DateTime dtbegin, DateTime dtend, string inhosid)
        {

            return CreateDAL<BAZADAL>().GetAllEntityByDate(dtbegin, dtend, inhosid);


        }

        public SqlExecuteResult DeleteAllEntityByDate(DateTime dtbegin, DateTime dtend, DbTransaction trans)
        {
            return CreateDAL<BAZADAL>().DeleteAllEntityByDate(dtbegin, dtend, trans);

        }


        public SqlExecuteResult DeleteAntInsertEntityByDate(DateTime dtBegin, DateTime dtEnd, string inhosid)
        {
            //DbTransaction trans = null;

            #region 从源数据库中获取要上传的区间段内的数据
            List<BAZAEntity> bazaList = CreateDAL<CommonDataDAL>().QueryBAZAEntityList(dtBegin, dtEnd, inhosid);

            List<BAF1Entity> baf1List = CreateDAL<CommonDataDAL>().QueryBAF1EntityList(dtBegin, dtEnd, inhosid);


            List<BAF2Entity> baf2List = CreateDAL<CommonDataDAL>().QueryBAF2EntityList(dtBegin, dtEnd, inhosid);

            List<BAF3Entity> baf3List = CreateDAL<CommonDataDAL>().QueryBAF3EntityList(dtBegin, dtEnd, inhosid);
            List<BAF4Entity> baf4List = CreateDAL<CommonDataDAL>().QueryBAF4EntityList(dtBegin, dtEnd, inhosid);
            List<BAF5Entity> baf5List = CreateDAL<CommonDataDAL>().QueryBAF5EntityList(dtBegin, dtEnd, inhosid);
            List<BAF9Entity> baf9List = CreateDAL<CommonDataDAL>().QueryBAF9EntityList(dtBegin, dtEnd, inhosid);
            List<BAFAEntity> bafAList = CreateDAL<CommonDataDAL>().QueryBAFAEntityList(dtBegin, dtEnd, inhosid);
            List<BAFBEntity> bafBList = CreateDAL<CommonDataDAL>().QueryBAFBEntityList(dtBegin, dtEnd, inhosid);
            List<BAFCEntity> bafCList = CreateDAL<CommonDataDAL>().QueryBAFCEntityList(dtBegin, dtEnd, inhosid);
            List<BAFDEntity> bafDList = CreateDAL<CommonDataDAL>().QueryBAFDEntityList(dtBegin, dtEnd, inhosid);

            #endregion

            #region 执行事务处理上传

            if (dtEnd == DateTime.MaxValue)
            {
                SqlExecuteResult result2 = CreateDAL<BAZADAL>().DeleteAllEntityByDate(inhosid, null);
            }
            else
            {
                //删除区间段内所有的表中的数据
                SqlExecuteResult result1 = CreateDAL<BAZADAL>().DeleteAllEntityByDate(dtBegin, dtEnd, null);
            }


            return DAOManager.ExecuteTransaction(trans =>
            {

                SqlExecuteResult result = new SqlExecuteResult();
                //循环新增BAZA表
                foreach (BAZAEntity item in bazaList)
                {
                    result = CreateDAL<BAZADAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }

                foreach (BAF1Entity item in baf1List)
                {
                    result = CreateDAL<BAF1DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAF2Entity item in baf2List)
                {
                    result = CreateDAL<BAF2DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAF3Entity item in baf3List)
                {
                    result = CreateDAL<BAF3DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAF4Entity item in baf4List)
                {
                    result = CreateDAL<BAF4DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAF5Entity item in baf5List)
                {
                    result = CreateDAL<BAF5DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAF9Entity item in baf9List)
                {
                    result = CreateDAL<BAF9DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAFAEntity item in bafAList)
                {
                    result = CreateDAL<BAFADAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAFBEntity item in bafBList)
                {
                    result = CreateDAL<BAFBDAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAFCEntity item in bafCList)
                {
                    result = CreateDAL<BAFCDAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }
                foreach (BAFDEntity item in bafDList)
                {
                    result = CreateDAL<BAFDDAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }



                return result;
            });
            #endregion


        }




        /*-------------------------------------------2011.06.08----------------------------------------------------*/
        /// <summary>
        /// 新腾冲医院报表导入程序  
        /// </summary>
        /// <param name="dtBegin"></param>
        /// <param name="dtEnd"></param>
        /// <param name="inhosid"></param>
        /// <returns></returns>
        public SqlExecuteResult DeleteInsertEntityByDate(DateTime dtBegin, DateTime dtEnd, string inhosid)
        {
            #region 从源数据库中获取要上传的区间段内的数据
            List<N04Entity> bazaList = CreateDAL<CommonDataDAL>().QueryN04EntityList(dtBegin, dtEnd, inhosid);
            #endregion

            #region 执行事务处理上传

            if (dtEnd == DateTime.MaxValue)
            {
                SqlExecuteResult result2 = CreateDAL<BAZADAL>().DeleteN04EntityByDate(inhosid, null);
            }
            else
            {
                //删除区间段内所有的表中的数据
                SqlExecuteResult result1 = CreateDAL<BAZADAL>().DeleteN04EntityByDate(dtBegin, dtEnd, null);
            }


            return DAOManager.ExecuteTransaction(trans =>
            {

                SqlExecuteResult result = new SqlExecuteResult();
                //循环新增BAZA表
                foreach (N04Entity item in bazaList)
                {
                    result = CreateDAL<N04DAL>().Add(item, trans);
                    if (!result.Success)
                    {
                        trans.Rollback();
                    }
                }

                return result;

            });
            #endregion


        }




    }
}
